#coding=utf-8
import pygame
from random import *
from memory_pic import *
from base64 import *

def get_pic(pic_code, pic_name):
    image = open(pic_name, 'wb')
    image.write(b64decode(pic_code))
    image.close()

get_pic(enemy1_png, "enemy1.png")
get_pic(enemy1_down1_png, "enemy1_down1.png")
get_pic(enemy1_down2_png, "enemy1_down2.png")
get_pic(enemy1_down3_png, "enemy1_down3.png")
get_pic(enemy1_down4_png, "enemy1_down4.png")
get_pic(enemy2_png, "enemy2.png")
get_pic(enemy2_down1_png, "enemy2_down1.png")
get_pic(enemy2_down2_png, "enemy2_down2.png")
get_pic(enemy2_down3_png, "enemy2_down3.png")
get_pic(enemy2_down4_png, "enemy2_down4.png")
get_pic(enemy2_hit_png, "enemy2_hit.png")
get_pic(enemy3_n1_png, "enemy3_n1.png")
get_pic(enemy3_n2_png, "enemy3_n2.png")
get_pic(enemy3_down1_png, "enemy3_down1.png")
get_pic(enemy3_down2_png, "enemy3_down2.png")
get_pic(enemy3_down3_png, "enemy3_down3.png")
get_pic(enemy3_down4_png, "enemy3_down4.png")
get_pic(enemy3_down5_png, "enemy3_down5.png")
get_pic(enemy3_down6_png, "enemy3_down6.png")
get_pic(enemy3_hit_png, "enemy3_hit.png")

class SmallEnemy(pygame.sprite.Sprite):
    def __init__(self, bg_size):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('enemy1.png').convert_alpha()
        self.image1 = pygame.image.load('enemy1_down1.png').convert_alpha()
        self.image2 = pygame.image.load('enemy1_down2.png').convert_alpha()
        self.image3 = pygame.image.load('enemy1_down3.png').convert_alpha()
        self.image4 = pygame.image.load('enemy1_down4.png').convert_alpha()
        self.destroy_images = []
        self.destroy_images.extend([self.image1, self.image2, self.image3, self.image4])
        self.rect = self.image.get_rect()
        self.width, self.height = bg_size[0], bg_size[1]
        self.speed = 2
        self.rect.left, self.rect.bottom = randint(0, self.width - self.rect.width), randint(-5 * self.height, 0)
        self.active = True
        self.mask = pygame.mask.from_surface(self.image)

    def move(self):
        if self.rect.top < self.height:
            self.rect.top += self.speed
        else:
            self.reset()
    def reset(self):
        self.active = True
        self.rect.left, self.rect.bottom = randint(0, self.width - self.rect.width), randint(-5 * self.height, 0)

class MidEnemy(pygame.sprite.Sprite):
    energy = 10

    def __init__(self, bg_size):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('enemy2.png').convert_alpha()
        self.image1 = pygame.image.load('enemy2_down1.png').convert_alpha()
        self.image2 = pygame.image.load('enemy2_down2.png').convert_alpha()
        self.image3 = pygame.image.load('enemy2_down3.png').convert_alpha()
        self.image4 = pygame.image.load('enemy2_down4.png').convert_alpha()
        self.image_hit = pygame.image.load('enemy2_hit.png').convert_alpha()
        self.destroy_images = []
        self.destroy_images.extend([self.image1, self.image2, self.image3, self.image4])
        self.rect = self.image.get_rect()
        self.width, self.height = bg_size[0], bg_size[1]
        self.rect.left, self.rect.bottom = randint(0, self.width - self.rect.width), randint(-10 * self.height, - self.height)
        self.speed = 1
        self.energy = MidEnemy.energy
        self.active = True
        self.hit = False
        self.mask = pygame.mask.from_surface(self.image)

    def move(self):
        if self.rect.top < self.height:
            self.rect.top += self.speed
        else:
            self.reset()
    def reset(self):
        self.active = True
        self.rect.left, self.rect.bottom = randint(0, self.width - self.rect.width), randint(-10 * self.height, 0)
        self.energy = MidEnemy.energy

class BigEnemy(pygame.sprite.Sprite):
    energy = 30

    def __init__(self, bg_size):
        pygame.sprite.Sprite.__init__(self)
        self.image1 = pygame.image.load('enemy3_n1.png').convert_alpha()
        self.image2 = pygame.image.load('enemy3_n2.png').convert_alpha()
        self.image3 = pygame.image.load('enemy3_down1.png').convert_alpha()
        self.image4 = pygame.image.load('enemy3_down2.png').convert_alpha()
        self.image5 = pygame.image.load('enemy3_down3.png').convert_alpha()
        self.image6 = pygame.image.load('enemy3_down4.png').convert_alpha()
        self.image7 = pygame.image.load('enemy3_down5.png').convert_alpha()
        self.image8 = pygame.image.load('enemy3_down6.png').convert_alpha()
        self.image_hit = pygame.image.load('enemy3_hit.png').convert_alpha()
        self.destroy_images = []
        self.destroy_images.extend([self.image3, self.image4, self.image5, self.image6, self.image7, self.image8])
        self.rect = self.image1.get_rect()
        self.width, self.height = bg_size[0], bg_size[1]
        self.rect.left, self.rect.bottom = randint(0, self.width - self.rect.width), randint(-15 * self.height, -5 * self.height)
        self.speed = 1
        self.energy = BigEnemy.energy
        self.active = True
        self.hit = False
        self.mask = pygame.mask.from_surface(self.image1)

    def move(self):
        if self.rect.top < self.height:
            self.rect.top += self.speed
        else:
            self.reset()
    def reset(self):
        self.active = True
        self.rect.left, self.rect.bottom = randint(0, self.width - self.rect.width), randint(-15 * self.height, -5 * self.height)
        self.energy = BigEnemy.energy